package com.fishsticks.view;



import com.fishsticks.model.DatabaseManager;
import com.fishsticks.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * Lets the user register a new username in To-Do app
 */
public class RegisterActivity extends Activity
{
	private String first_name, last_name, username, password, password_confirm;
	private EditText first_name_field, last_name_field, username_field, password_field, password_confirm_field;
	private Button cancel_button, register_button;
	private DatabaseManager dm;
	private AlertDialog.Builder reg_dialog;
	private AlertDialog rtn;
	private Bundle extras;
	public boolean test = false;
	
    /**
     * Called when the activity is first created.
     * Initializes button click listeners and intents
     * to switch back to login page
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        this.setContentView(R.layout.register_page);
        

        cancel_button = (Button) findViewById(R.id.cancelButtonRegisterPage);
        register_button = (Button) findViewById(R.id.registerButtonRegisterPage);
        
        first_name_field = ((EditText) findViewById(R.id.firstnameFieldRegisterPage));
        last_name_field = ((EditText) findViewById(R.id.lastnameFieldRegisterPage));
        username_field = ((EditText) findViewById(R.id.usernameFieldRegisterPage));
        password_field = ((EditText) findViewById(R.id.passwordFieldRegisterPage));
        password_confirm_field = ((EditText) findViewById(R.id.reenterPasswordFieldRegisterPage));
        
        
        
        // Gets recently created username if coming 
        // from the registration page
        extras = getIntent().getExtras();
        if(extras != null) 
        {
        	username_field.setText(extras.getString("username"));
        }
        

        cancel_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//Switches back to login screen
				Intent i = new Intent(getApplicationContext(), LoginActivity.class);
				startActivity(i);
				finish();
			}

        });

        register_button.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v){
        		first_name = first_name_field.getText().toString();
        		last_name = last_name_field.getText().toString();
        		username = username_field.getText().toString();
        		password = password_field.getText().toString();
        		password_confirm = password_confirm_field.getText().toString();

        		dm = new DatabaseManager(RegisterActivity.this);

        		int error_code = dm.register(first_name, last_name, username, password, password_confirm);
        		
        		if(error_code == R.string.registration_error_code_blank_fields)
        		{
        			reg_dialog = new AlertDialog.Builder(RegisterActivity.this, R.style.white_orange_dialog);
        			reg_dialog.setTitle("Registration Error");
        			reg_dialog.setMessage(getResources().getString(R.string.registration_error_message_blank_fields));
        			reg_dialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							rtn.dismiss();
						}
					});
        			rtn = reg_dialog.show();     			
        		}
        		
        		else if(error_code == R.string.registration_error_code_names_too_long)
        		{
        			reg_dialog = new AlertDialog.Builder(RegisterActivity.this, R.style.white_orange_dialog);
        			reg_dialog.setTitle("Registration Error");
        			reg_dialog.setMessage(getResources().getString(R.string.registration_error_message_names_too_long));
        			reg_dialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							rtn.dismiss();
						}
					});
        			rtn = reg_dialog.show();        			
        		}
        		
        		else if(error_code == R.string.registration_error_code_password_mismatch)
        		{
        			reg_dialog = new AlertDialog.Builder(RegisterActivity.this, R.style.white_orange_dialog);
        			reg_dialog.setTitle("Registration Error");
        			reg_dialog.setMessage(getResources().getString(R.string.registration_error_message_password_mismatch));
        			reg_dialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							rtn.dismiss();
						}
					});
        			rtn = reg_dialog.show();        			
        		}
        		
        		else if(error_code == R.string.registration_error_code_password_too_long)
        		{
        			reg_dialog = new AlertDialog.Builder(RegisterActivity.this, R.style.white_orange_dialog);
        			reg_dialog.setTitle("Registration Error");
        			reg_dialog.setMessage(getResources().getString(R.string.registration_error_message_password_too_long));
        			reg_dialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							rtn.dismiss();
						}
					});
        			rtn = reg_dialog.show();        			
        		}
        		
        		else
        		{
        			test = true;
        			reg_dialog = new AlertDialog.Builder(RegisterActivity.this, R.style.white_orange_dialog);
        			reg_dialog.setTitle("Registration Success!");
        			reg_dialog.setMessage("Thanks for registering. You may now log in.");
        			reg_dialog.setPositiveButton("Take me to log in!", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent i = new Intent(getApplicationContext(), LoginActivity.class);
							i.putExtra("username", username);
							startActivity(i);
						}
					});
        			reg_dialog.show();
        		}

        		dm.close();
        	}
        });
    }
}